﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm {
    class StoreObj {
        public bool m_bAlive;
        public Vector2 m_v2Position;
        public int m_iRow, m_iColumn;
        public int m_iIDBoom;
        public StoreObj() {
            m_v2Position = new Vector2(0, 0);
            m_bAlive = false;
            m_iRow = -1;
            m_iColumn = -1;
            m_iIDBoom = 0;
        }
    }

    class cManagerBoom {
        private cboom[] m_arrayBoom;
        public int m_iLength;
        public int m_iNumber;
        public int m_iCountNumber;
        private StoreObj[] m_arrayObjTemp;
        const int c_iMaxBoom = 10;
        public KeyboardState m_ksPreKeyState;
        public Texture2D m_tBoom;
        private int m_iy;
        private int m_ix;
        Rectangle m_rMid = new Rectangle(80, 80, 40, 40);
        Rectangle m_rLeft = new Rectangle(40, 80, 40, 40);
        Rectangle m_rTop = new Rectangle(80, 40, 40, 40);
        Rectangle m_rRight = new Rectangle(120, 80, 40, 40);
        Rectangle m_rBottom = new Rectangle(80, 120, 40, 40);
        Rectangle m_rEndLeft = new Rectangle(0, 80, 40, 40);
        Rectangle m_rEndTop = new Rectangle(80, 0, 40, 40);
        Rectangle m_rEndRight = new Rectangle(160, 80, 40, 40);
        Rectangle m_rEndBottom = new Rectangle(80, 160, 40, 40);

        public cManagerBoom(ContentManager content) {
            m_arrayObjTemp = new StoreObj[c_iMaxBoom * 10];
            for (int i = 0; i < c_iMaxBoom * 10; i++) {
                m_arrayObjTemp[i] = new StoreObj();
            }
            m_arrayBoom = new cboom[c_iMaxBoom];
            for (int i = 0; i < c_iMaxBoom; i++) {
                m_arrayBoom[i] = new cboom(content, new Vector2(0, 0), 1);
            }
            m_iLength = 1;
            m_iNumber = 1;
            m_iCountNumber = 0;
        }

        protected KeyboardState checkKeyboard(GameTime gameTime, KeyboardState keyboardState, Vector2 newPosition, int[, ,] map, int IDmap) {
            if ((keyboardState.IsKeyDown(Keys.Space)) && (m_ksPreKeyState.IsKeyUp(Keys.Space)) && (Game1.g_iEnergy > 0)) {
                foreach (cboom Boom in m_arrayBoom) {
                    if (map[IDmap, m_ix, m_iy] == 0 && (!Boom.m_bAlive) && (m_iCountNumber < m_iNumber)) {
                        m_iCountNumber++;
                        Boom.m_iCountLeft = m_iLength;
                        Boom.m_iCountRight = m_iLength;
                        Boom.m_iCountTop = m_iLength;
                        Boom.m_iCountBot = m_iLength;
                        Boom.m_bAlive = true;
                        Boom.m_v2Position = newPosition;
                        Game1.g_iEnergy--;
                        return keyboardState;
                    }
                }
            }
            return keyboardState;
        }

        public void Update(GameTime gameTime, KeyboardState keyState, ContentManager content, Vector2 newPosition, int[, ,] map, int ID) {
            m_iy = ((int)newPosition.X - 20) / 40;
            m_ix = ((int)newPosition.Y - 40) / 40;
            m_ksPreKeyState = checkKeyboard(gameTime, keyState, newPosition, map, ID);
            foreach (cboom Boom in m_arrayBoom) {
                if (Boom.m_bAlive) {
                    Boom.m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                    if (Boom.m_iTimeSinceLastFrame > Boom.m_mMillisecondsPerFrame) {
                        Boom.m_iTimeSinceLastFrame -= Boom.m_mMillisecondsPerFrame;
                        ++Boom.m_pCurrentFrame.X;
                        if (Boom.m_pCurrentFrame.X >= Boom.m_pSheetSize.X) {
                            Boom.m_pCurrentFrame.X = 0;
                            ++Boom.m_pCurrentFrame.Y;
                            if (Boom.m_pCurrentFrame.Y >= Boom.m_pSheetSize.Y)
                                Boom.m_pCurrentFrame.Y = 0;
                        }
                    }
                    Boom.m_tsTime += gameTime.ElapsedGameTime;
                    switch (Boom.ID) {
                        case 1:
                            m_iy = (int)(Boom.m_v2Position.X - 20) / 40;
                            m_ix = (int)(Boom.m_v2Position.Y - 40) / 40;
                            if (Boom.m_tsTime > TimeSpan.FromMilliseconds(100))
                                map[ID, m_ix, m_iy] = 1;
                            if (Boom.m_tsTime > TimeSpan.FromMilliseconds(2000)) {
                                Boom.ID = 2;
                                Boom.m_tsTime -= TimeSpan.FromMilliseconds(2000);
                                Boom.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                            }
                            break;
                        case 2:
                            if (Boom.m_tsTime > TimeSpan.FromMilliseconds(300)) {
                                m_iCountNumber--;
                                Boom.ID = 1;
                                Boom.m_bAlive = false;
                                Boom.m_tsTime -= TimeSpan.FromMilliseconds(300);
                                Boom.m_iCountLeft = m_iLength;
                                Boom.m_iCountRight = m_iLength;
                                Boom.m_iCountTop = m_iLength;
                                Boom.m_iCountBot = m_iLength;
                                m_iy = (int)(Boom.m_v2Position.X - 20) / 40;
                                m_ix = (int)(Boom.m_v2Position.Y - 40) / 40;
                                map[ID, m_ix, m_iy] = 0;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public void Draw(GameTime gameTime, ContentManager content, SpriteBatch spriteBatch, int[, ,] map, ManagerMap managerMap, cManagerItem listItem, int ID, ManagerEnemies managerEnemies, cWitch witch) {
            int m_iy, m_ix;
            int m_iSoThuTu = 0;
            float m_fBGLayerDepth;
            foreach (cboom Boom in m_arrayBoom) {
                if (Boom.m_bAlive) {
                    m_iSoThuTu++;
                    m_fBGLayerDepth = Boom.m_v2Position.Y / 600.0f;
                    switch (Boom.ID) {
                        case 1: {
                                spriteBatch.Draw(m_tBoom, Boom.m_v2Position,
                                new Rectangle(Boom.m_pCurrentFrame.X * Boom.m_pFrameSize.X,
                                Boom.m_pCurrentFrame.Y * Boom.m_pFrameSize.Y,
                                Boom.m_pFrameSize.X, Boom.m_pFrameSize.Y),
                                Color.White, 0, Vector2.Zero,
                                1, SpriteEffects.None, m_fBGLayerDepth);
                                break;
                            }
                        case 2: {
                                if (Boom.m_tsTime < TimeSpan.FromMilliseconds(150)) {
                                    spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position, m_rMid, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                    m_iy = (int)(Boom.m_v2Position.X - 20) / 40;
                                    m_ix = (int)(Boom.m_v2Position.Y - 40) / 40;
                                    CheckCollision(managerEnemies, m_ix, m_iy, witch);
                                    for (int i = 1; i <= Boom.m_iCountLeft; i++) {
                                        if (i < m_iLength)
                                            if ((m_iy - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && ((map[ID, m_ix, m_iy - i] == 0) || (map[ID, m_ix, m_iy - i] > 6 && map[ID, m_ix, m_iy - i] < 11))) {
                                                if (map[ID, m_ix, m_iy - i] > 6 && map[ID, m_ix, m_iy - i] < 11) {
                                                    foreach (citem Item in listItem.m_arrayItem) {
                                                        if (Item.m_iRow == m_ix && Item.m_iColumn == (m_iy - i))
                                                            Item.m_bAlive = false;
                                                    }
                                                    map[ID, m_ix, m_iy - i] = 0;
                                                }
                                                CheckCollision(managerEnemies, m_ix, m_iy - i, witch);
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(i * 40, 0), m_rLeft, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            } else {
                                                if ((m_iy - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && map[ID, m_ix, m_iy - i] < 11) {
                                                    if (map[ID, m_ix, m_iy - i] == 1) {
                                                        foreach (cboom Boom2 in m_arrayBoom)
                                                            if ((new Vector2((m_iy - i) * 40 + 20, m_ix * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                                Boom2.ID = 2;
                                                                Boom2.m_tsTime = Boom.m_tsTime;
                                                                Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                            }
                                                        break;
                                                    } else
                                                        foreach (StoreObj obj in m_arrayObjTemp) {
                                                            if (obj.m_bAlive) {
                                                                if (obj.m_iRow == m_ix && obj.m_iColumn == m_iy - i) {
                                                                    obj.m_iIDBoom = m_iSoThuTu;
                                                                    break;
                                                                }
                                                            } else {
                                                                obj.m_v2Position = Boom.m_v2Position;
                                                                obj.m_bAlive = true;
                                                                obj.m_iRow = m_ix;
                                                                obj.m_iColumn = m_iy - i;
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        }
                                                    Boom.m_iCountLeft = i;
                                                }
                                                break;
                                            } else {
                                            if ((m_iy - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix, m_iy - i] > 0) && map[ID, m_ix, m_iy - i] < 6) {
                                                if (map[ID, m_ix, m_iy - i] == 1) {
                                                    foreach (cboom Boom2 in m_arrayBoom)
                                                        if ((new Vector2((m_iy - i) * 40 + 20, m_ix * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                            Boom2.ID = 2;
                                                            Boom2.m_tsTime = Boom.m_tsTime;
                                                            Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                        }
                                                } else
                                                    foreach (StoreObj obj in m_arrayObjTemp) {
                                                        if (obj.m_bAlive) {
                                                            if (obj.m_iRow == m_ix && obj.m_iColumn == m_iy - i) {
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        } else {
                                                            obj.m_v2Position = Boom.m_v2Position;
                                                            obj.m_bAlive = true;
                                                            obj.m_iRow = m_ix;
                                                            obj.m_iColumn = m_iy - i;
                                                            obj.m_iIDBoom = m_iSoThuTu;
                                                            break;
                                                        }
                                                    }
                                                Boom.m_iCountLeft = i;
                                                break;
                                            } else
                                                if ((m_iy - i) >= 0 && ((map[ID, m_ix, m_iy - i] == 0) || (map[ID, m_ix, m_iy - i] > 6 && map[ID, m_ix, m_iy - i] < 11))) {
                                                    if (map[ID, m_ix, m_iy - i] > 6 && map[ID, m_ix, m_iy - i] < 11) {
                                                        foreach (citem Item in listItem.m_arrayItem) {
                                                            if (Item.m_iRow == m_ix && Item.m_iColumn == (m_iy - i)) {
                                                                Item.m_iRow = -1;
                                                                Item.m_iColumn = -1;
                                                                Item.m_bAlive = false;
                                                            }
                                                        }
                                                        map[ID, m_ix, m_iy - i] = 0;
                                                    }
                                                    CheckCollision(managerEnemies, m_ix, m_iy - i, witch);
                                                    spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(m_iLength * 40, 0), m_rEndLeft, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                                }
                                        }

                                    }
                                    for (int i = 1; i <= Boom.m_iCountTop; i++) {
                                        if (i < m_iLength)

                                            if ((m_ix - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && ((map[ID, m_ix - i, m_iy] == 0) || (map[ID, m_ix - i, m_iy] > 6 && map[ID, m_ix - i, m_iy] < 11))) {
                                                if (map[ID, m_ix - i, m_iy] > 6 && map[ID, m_ix - i, m_iy] < 11) {
                                                    foreach (citem Item in listItem.m_arrayItem) {
                                                        if (Item.m_iRow == (m_ix - i) && Item.m_iColumn == m_iy)
                                                            Item.m_bAlive = false;
                                                    }
                                                    map[ID, m_ix - i, m_iy] = 0;
                                                }
                                                CheckCollision(managerEnemies, m_ix - i, m_iy, witch);
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(0, i * 40), m_rTop, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            } else {
                                                if ((m_ix - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && map[ID, m_ix - i, m_iy] < 11) {
                                                    if (map[ID, m_ix - i, m_iy] == 1) {
                                                        foreach (cboom Boom2 in m_arrayBoom)
                                                            if ((new Vector2(m_iy * 40 + 20, (m_ix - i) * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                                Boom2.ID = 2;
                                                                Boom2.m_tsTime = Boom.m_tsTime;
                                                                Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                            }
                                                    } else
                                                        foreach (StoreObj obj in m_arrayObjTemp) {
                                                            if (obj.m_bAlive) {
                                                                if (obj.m_iRow == m_ix - i && obj.m_iColumn == m_iy) {
                                                                    obj.m_iIDBoom = m_iSoThuTu;
                                                                    break;
                                                                }
                                                            } else {
                                                                obj.m_v2Position = Boom.m_v2Position;
                                                                obj.m_bAlive = true;
                                                                obj.m_iRow = m_ix - i;
                                                                obj.m_iColumn = m_iy;
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        }
                                                    Boom.m_iCountTop = i;
                                                }
                                                break;
                                            } else {
                                            if ((m_ix - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix - i, m_iy] > 0) && map[ID, m_ix - i, m_iy] < 6) {
                                                if (map[ID, m_ix - i, m_iy] == 1) {
                                                    foreach (cboom Boom2 in m_arrayBoom)
                                                        if ((new Vector2(m_iy * 40 + 20, (m_ix - i) * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                            Boom2.ID = 2;
                                                            Boom2.m_tsTime = Boom.m_tsTime;
                                                            Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                        }
                                                } else
                                                    foreach (StoreObj obj in m_arrayObjTemp) {
                                                        if (obj.m_bAlive) {
                                                            if (obj.m_iRow == m_ix - i && obj.m_iColumn == m_iy) {
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        } else {
                                                            obj.m_v2Position = Boom.m_v2Position;
                                                            obj.m_bAlive = true;
                                                            obj.m_iRow = m_ix - i;
                                                            obj.m_iColumn = m_iy;
                                                            obj.m_iIDBoom = m_iSoThuTu;
                                                            break;
                                                        }
                                                    }
                                                Boom.m_iCountTop = i;
                                                break;
                                            } else
                                                if ((m_ix - i) >= 0 && (map[ID, m_ix - i, m_iy] == 0 || (map[ID, m_ix - i, m_iy] > 6 && map[ID, m_ix - i, m_iy] < 11))) {
                                                    if (map[ID, m_ix - i, m_iy] > 6 && map[ID, m_ix - i, m_iy] < 11) {
                                                        foreach (citem Item in listItem.m_arrayItem) {
                                                            if (Item.m_iRow == (m_ix - i) && Item.m_iColumn == m_iy)
                                                                Item.m_bAlive = false;
                                                        }
                                                        map[ID, m_ix - i, m_iy] = 0;
                                                    }
                                                    CheckCollision(managerEnemies, m_ix - i, m_iy, witch);
                                                    spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(0, m_iLength * 40), m_rEndTop, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                                }
                                        }
                                    }
                                    for (int i = 1; i <= Boom.m_iCountRight; i++) {
                                        if (i < m_iLength)
                                            if ((m_iy + i) <= 14 && (m_iy >= 0 && m_ix >= 0) && ((map[ID, m_ix, m_iy + i] == 0) || (map[ID, m_ix, m_iy + i] > 6 && map[ID, m_ix, m_iy + i] < 11))) {
                                                if (map[ID, m_ix, m_iy + i] > 6 && map[ID, m_ix, m_iy + i] < 11) {
                                                    foreach (citem Item in listItem.m_arrayItem) {
                                                        if (Item.m_iRow == m_ix && Item.m_iColumn == (m_iy + i))
                                                            Item.m_bAlive = false;
                                                    }
                                                    map[ID, m_ix, m_iy + i] = 0;
                                                }
                                                CheckCollision(managerEnemies, m_ix, m_iy + i, witch);
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(i * 40, 0), m_rRight, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            } else {
                                                if ((m_iy + i) <= 14 && (m_iy >= 0 && m_ix >= 0) && map[ID, m_ix, m_iy + i] < 11) {
                                                    if (map[ID, m_ix, m_iy + i] == 1) {
                                                        foreach (cboom Boom2 in m_arrayBoom)
                                                            if ((new Vector2((m_iy + i) * 40 + 20, m_ix * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                                Boom2.ID = 2;
                                                                Boom2.m_tsTime = Boom.m_tsTime;
                                                                Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                            }
                                                    } else
                                                        foreach (StoreObj obj in m_arrayObjTemp) {
                                                            if (obj.m_bAlive) {
                                                                if (obj.m_iRow == m_ix && obj.m_iColumn == m_iy + i) {
                                                                    obj.m_iIDBoom = m_iSoThuTu;
                                                                    break;
                                                                }
                                                            } else {
                                                                obj.m_v2Position = Boom.m_v2Position;
                                                                obj.m_bAlive = true;
                                                                obj.m_iRow = m_ix;
                                                                obj.m_iColumn = m_iy + i;
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        }
                                                    Boom.m_iCountRight = i;
                                                }
                                                break;
                                            } else {
                                            if ((m_iy + i) <= 14 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix, m_iy + i] > 0) && map[ID, m_ix, m_iy + i] < 6) {
                                                if (map[ID, m_ix, m_iy + i] == 1) {
                                                    foreach (cboom Boom2 in m_arrayBoom)
                                                        if ((new Vector2((m_iy + i) * 40 + 20, m_ix * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                            Boom2.ID = 2;
                                                            Boom2.m_tsTime = Boom.m_tsTime;
                                                            Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                        }
                                                } else
                                                    foreach (StoreObj obj in m_arrayObjTemp) {
                                                        if (obj.m_bAlive) {
                                                            if (obj.m_iRow == m_ix && obj.m_iColumn == m_iy + i) {
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        } else {
                                                            obj.m_v2Position = Boom.m_v2Position;
                                                            obj.m_bAlive = true;
                                                            obj.m_iRow = m_ix;
                                                            obj.m_iColumn = m_iy + i;
                                                            obj.m_iIDBoom = m_iSoThuTu;
                                                            break;
                                                        }
                                                    }
                                                Boom.m_iCountRight = i;
                                                break;
                                            }
                                            if ((m_iy + i) <= 14 && ((map[ID, m_ix, m_iy + i] == 0) || (map[ID, m_ix, m_iy + i] > 6 && map[ID, m_ix, m_iy + i] < 11))) {
                                                if (map[ID, m_ix, m_iy + i] > 6 && map[ID, m_ix, m_iy + i] < 11) {
                                                    foreach (citem Item in listItem.m_arrayItem) {
                                                        if (Item.m_iRow == m_ix && Item.m_iColumn == (m_iy + i))
                                                            Item.m_bAlive = false;
                                                    }
                                                    map[ID, m_ix, m_iy + i] = 0;
                                                }
                                                CheckCollision(managerEnemies, m_ix, m_iy + i, witch);
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(m_iLength * 40, 0), m_rEndRight, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            }
                                        }
                                    }
                                    for (int i = 1; i <= Boom.m_iCountBot; i++) {
                                        if (i < m_iLength)
                                            if ((m_ix + i) <= 12 && (m_iy >= 0 && m_ix >= 0) && ((map[ID, m_ix + i, m_iy] == 0) || (map[ID, m_ix + i, m_iy] > 6 && map[ID, m_ix + i, m_iy] < 11))) {
                                                if (map[ID, m_ix + i, m_iy] > 6 && map[ID, m_ix + i, m_iy] < 11) {
                                                    foreach (citem Item in listItem.m_arrayItem) {
                                                        if (Item.m_iRow == (m_ix + i) && Item.m_iColumn == m_iy)
                                                            Item.m_bAlive = false;
                                                    }
                                                    map[ID, m_ix + i, m_iy] = 0;
                                                }
                                                CheckCollision(managerEnemies, m_ix + i, m_iy, witch);
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(0, i * 40), m_rBottom, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            } else {
                                                if ((m_ix + i) <= 12 && (m_iy >= 0 && m_ix >= 0) && map[ID, m_ix + i, m_iy] < 11) {
                                                    if (map[ID, m_ix + i, m_iy] == 1) {
                                                        foreach (cboom Boom2 in m_arrayBoom)
                                                            if ((new Vector2(m_iy * 40 + 20, (m_ix + i) * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                                Boom2.ID = 2;
                                                                Boom2.m_tsTime = Boom.m_tsTime;
                                                                Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                            }
                                                    } else
                                                        foreach (StoreObj obj in m_arrayObjTemp) {
                                                            if (obj.m_bAlive) {
                                                                if (obj.m_iRow == m_ix + i && obj.m_iColumn == m_iy) {
                                                                    obj.m_iIDBoom = m_iSoThuTu;
                                                                    break;
                                                                }
                                                            } else {
                                                                obj.m_v2Position = Boom.m_v2Position;
                                                                obj.m_bAlive = true;
                                                                obj.m_iRow = m_ix + i;
                                                                obj.m_iColumn = m_iy;
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        }
                                                    Boom.m_iCountBot = i;
                                                }
                                                break;
                                            } else {
                                            if ((m_ix + i) <= 12 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix + i, m_iy] > 0) && map[ID, m_ix + i, m_iy] < 6) {
                                                if (map[ID, m_ix + i, m_iy] == 1) {
                                                    foreach (cboom Boom2 in m_arrayBoom)
                                                        if ((new Vector2(m_iy * 40 + 20, (m_ix + i) * 40 + 40) == Boom2.m_v2Position) && Boom2.m_bAlive) {
                                                            Boom2.ID = 2;
                                                            Boom2.m_tsTime = Boom.m_tsTime;
                                                            Boom2.m_tTexture1 = content.Load<Texture2D>(@"images/boom-new");
                                                        }
                                                } else
                                                    foreach (StoreObj obj in m_arrayObjTemp) {
                                                        if (obj.m_bAlive) {
                                                            if (obj.m_iRow == m_ix + i && obj.m_iColumn == m_iy) {
                                                                obj.m_iIDBoom = m_iSoThuTu;
                                                                break;
                                                            }
                                                        } else {
                                                            obj.m_v2Position = Boom.m_v2Position;
                                                            obj.m_bAlive = true;
                                                            obj.m_iRow = m_ix + i;
                                                            obj.m_iColumn = m_iy;
                                                            obj.m_iIDBoom = m_iSoThuTu;
                                                            break;
                                                        }
                                                    }
                                                Boom.m_iCountBot = i;
                                                break;
                                            } else
                                                if ((m_ix + i) <= 12 && (map[ID, m_ix + i, m_iy] == 0 || (map[ID, m_ix + i, m_iy] > 6 && map[ID, m_ix + i, m_iy] < 11))) {
                                                    if (map[ID, m_ix + i, m_iy] > 6 && map[ID, m_ix + i, m_iy] < 11) {
                                                        foreach (citem Item in listItem.m_arrayItem) {
                                                            if (Item.m_iRow == (m_ix + i) && Item.m_iColumn == m_iy)
                                                                Item.m_bAlive = false;
                                                        }
                                                        map[ID, m_ix + i, m_iy] = 0;
                                                    }
                                                    CheckCollision(managerEnemies, m_ix + i, m_iy, witch);
                                                    spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(0, m_iLength * 40), m_rEndBottom, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                                }
                                        }
                                    }
                                } else {
                                    Rectangle m_rLeft = new Rectangle(40, 40, 40, 40);
                                    Rectangle m_rRop = new Rectangle(120, 40, 40, 40);
                                    Rectangle m_rRight = new Rectangle(120, 120, 40, 40);
                                    Rectangle m_rBottom = new Rectangle(40, 120, 40, 40);
                                    m_iy = (int)(Boom.m_v2Position.X - 20) / 40;
                                    m_ix = (int)(Boom.m_v2Position.Y - 40) / 40;
                                    for (int i = 1; i <= Boom.m_iCountLeft; i++) {
                                        if (i < m_iLength)
                                            if ((m_iy - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && ((map[ID, m_ix, m_iy - i] == 0) || (map[ID, m_ix, m_iy - i] > 6 && map[ID, m_ix, m_iy - i] < 11)))
                                                spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(i * 40, 0), m_rLeft, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                            else break;
                                    }

                                    for (int i = 1; i <= Boom.m_iCountTop; i++) {
                                        if ((m_ix - i) >= 0 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix - i, m_iy] == 0))
                                            spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position - new Vector2(0, i * 40), m_rRop, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                        else break;
                                    }
                                    for (int i = 1; i <= Boom.m_iCountRight; i++) {
                                        if ((m_iy + i) <= 14 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix, m_iy + i] == 0))
                                            spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(i * 40, 0), m_rRight, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                        else break;
                                    }
                                    for (int i = 1; i <= Boom.m_iCountBot; i++) {
                                        if ((m_ix + i) <= 12 && (m_iy >= 0 && m_ix >= 0) && (map[ID, m_ix + i, m_iy] == 0))
                                            spriteBatch.Draw(Boom.m_tTexture1, Boom.m_v2Position + new Vector2(0, i * 40), m_rBottom, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                                        else break;
                                    }
                                }
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
            managerMap.Update(m_arrayObjTemp, gameTime, listItem, m_arrayBoom);
        }

        public void CheckCollision(ManagerEnemies managerEnemies, int row, int column, cWitch witch) {
            Rectangle m_rBoomRect = new Rectangle(
                                column * 40 + 20,
                                row * 40 + 40,
                                40,
                                40);
            Rectangle m_rWitchRect = new Rectangle(
                                (int)witch.m_v2Position.X + 13,
                                (int)witch.m_v2Position.Y + 35,
                                20,
                                20);
            foreach (cEnemies enemies in managerEnemies.l_Enemies)
                if (enemies.m_bAlive) {
                    Rectangle m_rEnemyRect = new Rectangle(
                                (int)enemies.m_v2Position.X + 10,
                                (int)enemies.m_v2Position.Y + 10,
                                20,
                                20);
                    if (m_rWitchRect.Intersects(m_rEnemyRect)) {
                        witch.m_iLife = 1;
                    }
                    if (m_rBoomRect.Intersects(m_rEnemyRect)) {
                        if (enemies.m_iIDlife == 2) {
                            enemies.m_iIDlife = 1;
                            Game1.g_iScore += 25;
                        }
                        if (enemies.m_iIDlife == 3) {
                            enemies.m_iIDlife = 4;
                            Game1.g_iScore += 25;
                        }

                    }
                }
            if (m_rBoomRect.Intersects(m_rWitchRect) && (witch.m_ttWitchLight.m_iIDFrame > 11)) {
                if (witch.m_iLife == 3) {
                    witch.m_iLife = 2;
                    witch.m_ttWitchLight.m_iIDFrame = 0;
                    return;
                }
                if (witch.m_iLife == 2) {
                    witch.m_iLife = 1;
                    witch.m_ttWitchLight.m_iIDFrame = 0;
                    return;
                }
            }
        }
    }

    class cboom {
        public Texture2D m_tTexture1 { get; set; }
        public Texture2D m_tTexture2 { get; set; }
        public Vector2 m_v2Position { get; set; }
        public bool m_bAlive;
        public int m_iTimeSinceLastFrame;
        public int m_mMillisecondsPerFrame;
        public Point m_pFrameSize;
        public Point m_pCurrentFrame;
        public Point m_pSheetSize;
        public int m_iCountLeft;
        public int m_iCountRight;
        public int m_iCountTop;
        public int m_iCountBot;
        public TimeSpan m_tsTime;
        public int ID;

        public cboom(ContentManager content, Vector2 newPosition, int IDBoom) {
            m_tTexture1 = content.Load<Texture2D>(@"images/boom-motmat");
            m_tTexture2 = content.Load<Texture2D>(@"images/boom-ranri");
            m_v2Position = newPosition;
            m_bAlive = false;
            m_iTimeSinceLastFrame = 0;
            m_mMillisecondsPerFrame = 200;
            m_pFrameSize = new Point(40, 40);
            m_pCurrentFrame = new Point(0, 0);
            m_pSheetSize = new Point(3, 1);
            m_iCountLeft = 1;
            m_iCountRight = 1;
            m_iCountTop = 1;
            m_iCountBot = 1;
            m_tsTime = TimeSpan.Zero;
            this.ID = IDBoom;
        }
    }
}